package com.hundsun.yr.universal.library.view.banner;

import java.util.List;
import java.util.concurrent.locks.LockSupport;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.hundsun.yr.universal.library.R;
import com.hundsun.yr.universal.library.adapter.HolderPagerAdapter;
import com.hundsun.yr.universal.library.adapter.ViewCreatorHelper;



/**
 * @Title: BannerAutoLoopLayout.java 
 * @Description: banner视图
 * @date 2015-12-4 上午9:45:43 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
public class BannerAutoLoopLayout<T> extends FrameLayout{
	
	private LoopViewPager loopViewPager;
    private CirclePageIndicator loop_indicator;
    private HolderPagerAdapter<T> mAdapter;
    private Context mContext;
    /**
     * 是否循环
     */
	private boolean ifLoop ;
	public BannerAutoLoopLayout(Context context) {
        this(context, null);
    }

    public BannerAutoLoopLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
       
    }
    
    public BannerAutoLoopLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    	super(context, attrs, defStyleAttr);
    	init(context, attrs, defStyleAttr);
    }
    
    
    private void init(Context context, AttributeSet attrs, int defStyleAttr){
    	mContext=context;
    	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	inflater.inflate(R.layout.view_loop_banner, this, true);
    	loopViewPager=(LoopViewPager)findViewById(R.id.loop_pager);
    	loop_indicator=(CirclePageIndicator)findViewById(R.id.loop_indicator);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirclePageIndicator, defStyleAttr, 0);
        
        if(a.hasValue(R.styleable.CirclePageIndicator_strokeColor)){
        	
        	int strokeColor=a.getColor(R.styleable.CirclePageIndicator_strokeColor, getResources().getColor(R.color.default_circle_indicator_stroke_color));
        	setStrokeColor(strokeColor);
        }
        
        
        if(a.hasValue(R.styleable.CirclePageIndicator_pageColor)){
        	
        	int pageColor=a.getColor(R.styleable.CirclePageIndicator_pageColor, getResources().getColor(R.color.default_circle_indicator_page_color));
        	setPageColor(pageColor);
        }
        
        
        if(a.hasValue(R.styleable.CirclePageIndicator_strokeWidth)){
        	
        	float strokeWidth = a.getDimension(R.styleable.CirclePageIndicator_strokeWidth, getResources().getDimension(R.dimen.default_circle_indicator_stroke_width));
        	setStrokeWidth(strokeWidth);
        }
        
        if(a.hasValue(R.styleable.CirclePageIndicator_fillColor)){
        	int fillColor=a.getColor(R.styleable.CirclePageIndicator_fillColor, getResources().getColor(R.color.default_circle_indicator_fill_color));
        	setFillColor(fillColor);
        }
//        if(a.hasValue(R.styleable.CirclePageIndicator_isloop)){
        	ifLoop = a.getBoolean(R.styleable.CirclePageIndicator_isloop,true);
//        }
        if(a.hasValue(R.styleable.CirclePageIndicator_circleBottomLength)){
        	float paddingBottom = a.getDimension(R.styleable.CirclePageIndicator_circleBottomLength, getResources().getDimension(R.dimen.default_circle_indicator_circleBottomLength));
        	loop_indicator.setPadding(0, 0, 0, (int)paddingBottom);
        }

        a.recycle();
    	
    }
    
    /**
     * 设置是否循环
     * @param ifloop
     */
    public void setLoop(boolean ifloop) {
		this.ifLoop = ifloop;
	}

	/**
     * 设置数据
     * @param context
     * @param layout
     * @param data
     * @param updateItem
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public void setAdapter(int layout,ViewCreatorHelper.UpdateItem<T> updateItem){
    	ViewCreatorHelper creatorHelper=new ViewCreatorHelper(layout, updateItem);
    	mAdapter=new HolderPagerAdapter<T>(mContext,creatorHelper.viewCreator());
    	
    }
    
    /**
     * 刷新数据
     */
    public void updata(List<T> data){
    	
    	if(mAdapter!=null&&data!=null&&data.size()>0){
    		mAdapter.update(data);
			loopViewPager.setAdapter(mAdapter,ifLoop);
//			loopViewPager.setAdapter(mAdapter);
    		loop_indicator.setViewPager(loopViewPager);
    	}
    }
    
    /**
     * 开始自动轮播
     * @param interval
     * @param stopScrollWhenTouch
     */
    public void startAutoScroll(long interval , boolean stopScrollWhenTouch){
    	loopViewPager.startAutoScroll();
		loopViewPager.setInterval(interval);
		loopViewPager.setStopScrollWhenTouch(stopScrollWhenTouch);
    }
    
    
    /**
     * 开始自动轮播
     */
    public void startAutoScroll(){
    	loopViewPager.startAutoScroll();
		loopViewPager.setStopScrollWhenTouch(true);
    }
    
    /**
     * 停止
     */
    public void stopAutoScroll(){
    	loopViewPager.stopAutoScroll();
    }
    /**
     * 设置LoopViewPager的高度
     * @param height
     */
    public void setHeight(int height){
    	
    	loopViewPager.getLayoutParams().height=height;
    }
    
    
    /**
     * 设置圆圈边框颜色
     * @param strokeColor
     */
    public void setStrokeColor(int strokeColor) {
    	loop_indicator.setStrokeColor(strokeColor);
    }
    
    /**
     * 设置圆点边框宽度
     * @param strokeWidth
     */
    public void setStrokeWidth(float strokeWidth) {
    	loop_indicator.setStrokeWidth(strokeWidth);
    }
    
    /**
     * 设置圆点填充色
     * @param fillColor
     */
    public void setFillColor(int fillColor) {
    	loop_indicator.setFillColor(fillColor);
    }

    
    /**
     * 设置默认圆点填充色
     * @param fillColor
     */
    public void setPageColor(int pageColor) {
    	loop_indicator.setPageColor(pageColor);
    }
    
    public void setOrientation(int orientation) {
    	loop_indicator.setOrientation(orientation);
    }
    
    /**
     * 获取loopViewPager
     * @return
     */
    public LoopViewPager getLoopViewPager(){
    	
    	return loopViewPager;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.e("banner: ",super.dispatchTouchEvent(ev) + "");
        return super.dispatchTouchEvent(ev);
    }
}
